package com.starblink.library.widget.novice_guide.home

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.skydoves.balloon.BalloonGuide
import com.starblink.library.widget.novice_guide.control.GuidePopStatusControl
import com.starblink.android.basic.ext.*
import com.starblink.android.basic.extension.dp2px
import com.starblink.android.basic.guide.SkNoviceManage
import com.starblink.android.basic.guide.bean.SkGuideBusConfig
import com.starblink.android.basic.guide.bean.SkGuideEnum
import com.starblink.android.basic.sensorsdata.room.entity.SkAntEntity
import com.starblink.android.basic.sensorsdata.spm.GTrackerAssistUtils
import com.starblink.android.basic.sensorsdata.spm.cfg.SpmElementDef
import com.starblink.android.basic.sensorsdata.spm.event.TrackEvent
import com.starblink.android.basic.sensorsdata.spm.impl.SpmTrackHandler
import com.starblink.android.basic.util.bus.FlowBus
import com.starblink.basic.style.R
import com.starblink.library.widget.databinding.AddVColl0ToastBinding

object FirstProductCollPop {


    fun pop(
        anchorView: View,
        parent: ViewGroup,
        spmPage: Int
    ) {
        if (!SkNoviceManage.canShowGuide(SkGuideEnum.HOME_FRG_1_PRODUCT_COLL_V2)) {
            return
        }
        GuidePopStatusControl.getHollowRectFWindow(anchorView)?.let { rectF ->
            //在右边 显示不下 不显示
            if (rectF.left > (375 / 2).dp2px()) {
                return
            }
            //↓
            val toastLay =
                AddVColl0ToastBinding.inflate(
                    LayoutInflater.from(anchorView.context),
                    parent,
                    false
                )

            //收藏的图标宽

            //遮盖居中盖上去
            toastLay.btm.margin(
                topMargin = (rectF.top).toInt(),
                leftMargin = (rectF.left).toInt()
            )
            //↑
            var balloon = BalloonGuide.Builder(parent.context)
                .setLayout(toastLay)
                .setPadding(0)
                .setOnBalloonDismissListener {
//                    FlowBus.boolFlowBusNotify(SkGuideBusConfig.HOME_FRG_2_PRODUCT_V2_NAME)
                }.setOnBalloonClickListener {
                    SpmTrackHandler.addSingleTrackData(
                        SkAntEntity(
                            spmCnt = GTrackerAssistUtils.fetchElementValue(
                                spmPage,
                                SpmElementDef.Element_33069
                            ), params = mapOf("function" to "add")
                        )
                    )
                }
                .setBackgroundColorResource(R.color.transparent)
                .build()
            balloon.showFull(parent)

            SkNoviceManage.hasPopGuide(SkGuideEnum.HOME_FRG_1_PRODUCT_COLL_V2)
            SpmTrackHandler.addSingleTrackData(
                SkAntEntity(
                    event = TrackEvent.elementExposure.value,
                    spmCnt = GTrackerAssistUtils.fetchElementValue(
                        spmPage,
                        SpmElementDef.Element_33069
                    ), params = mapOf("function" to "add")
                )
            )
        }

    }

}